package com.isczy.eduservice.controller;


import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isczy.eduservice.entity.EduCourse;
import com.isczy.eduservice.entity.vo.CourseInfoVo;
import com.isczy.eduservice.entity.vo.CourseQueryVo;
import com.isczy.eduservice.service.EduCourseService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程信息 前端控制器
 * </p>
 *
 * @author CZY
 * @since 2021-01-02
 */
@Api(description = "课程基本信息 前端控制器")
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "分页查询课程基本信息")
    @SentinelResource(value = "getCourseList")
    @PostMapping("getCourseList/{current}/{limit}")
    public DataResult getCourseList(@ApiParam(name = "current",value = "当前页码数",required = true)@PathVariable Long current,
                                  @ApiParam(name = "limit",value = "每页显示条数",required = true)@PathVariable Long limit,
                                  @RequestBody(required = false) CourseQueryVo courseQueryVo){

        try {
            String title = courseQueryVo.getTitle();
            String status = courseQueryVo.getStatus();
            String begin = courseQueryVo.getBegin();
            String end = courseQueryVo.getEnd();

            QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
            if (StrUtil.isNotEmpty(title)){
                wrapper.like("title",title);
            }
            if (StrUtil.isNotEmpty(status)){
                wrapper.eq("status",status);
            }
            if (StrUtil.isNotEmpty(begin)){
                wrapper.ge("gmt_create",begin);
            }
            if (StrUtil.isNotEmpty(end)){
                wrapper.le("gmt_create",end);
            }
            wrapper.orderByDesc("gmt_create");
            //创建分页对象
            //调用方法实现，底层将查询出的数据封装到pageTeacher对象中
            Page<EduCourse> pageCourse = courseService.page(new Page<>(current, limit), wrapper);

            return DataResult.builder()
                    .data("total",pageCourse.getTotal())
                    .data("data",pageCourse.getRecords())
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("条件分页查询讲师失败")
                    .params(ArrayUtil.toString(new Object[]{current,limit,courseQueryVo}))
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述:添加课程基本信息
     * @param courseInfoVo
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-07 11:00】
     * @author modify:
     */
    @ApiOperation(value = "添加课程基本信息")
    @PostMapping("addCourseInfo")
    public DataResult addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        try {
            return DataResult.builder()
                    .data(courseService.addCourseInfo(courseInfoVo))
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("添加课程基本信息失败")
                    .params(courseInfoVo)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据课程id查询课程基本信息
     * @param courseId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 14:28】
     * @author modify:
     */
    @ApiOperation(value = "根据课程id查询课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public DataResult getCourseInfo(@PathVariable String courseId) {
        try {
            return DataResult.builder()
                    .data(courseService.getCourseInfo(courseId))
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据课程id查询课程基本信息失败")
                    .params(courseId)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:修改课程信息
     * @param courseInfoVo
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 14:49】
     * @author modify:
     */
    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourseInfo")
    public DataResult updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        try {
            courseService.updateCourseInfo(courseInfoVo);
            return DataResult.builder()
                    .data(courseInfoVo.getId())
                    .message("修改课程信息成功")
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("修改课程信息失败")
                    .params(courseInfoVo)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据课程id查询课程确认信息
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 14:50】
     * @author modify:
     */
    @ApiOperation(value = "根据课程id查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public DataResult getPublishCourseInfo(@PathVariable String id) {
        try {
            return DataResult.builder()
                    .data(courseService.publishCourseInfo(id))
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据课程id查询课程确认信息失败")
                    .params(id)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:课程最终发布
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-06 16:37】
     * @author modify:
     */
    @GetMapping("publishCourse/{id}")
    public DataResult publishCourse(@PathVariable String id) {
        try {
            EduCourse eduCourse = new EduCourse();
            eduCourse.setId(id);
            eduCourse.setStatus("Normal");//设置课程发布状态
            courseService.updateById(eduCourse);
            return DataResult.builder().successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("课程最终发布失败")
                    .params(id)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:删除课程
     * @param courseId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-06 16:40】
     * @author modify:
     */
    @DeleteMapping("deleteById/{courseId}")
    public DataResult deleteCourse(@PathVariable String courseId) {
        try {
            courseService.removeCourse(courseId);
            return DataResult.builder().successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("删除课程失败")
                    .params(courseId)
                    .build();
        }
    }



}

